﻿using CarAppDomain.Rule.Entities;
using CarAppDomain.Rule.IRepositories; 
using D.UtilSqlsugarOrm;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;

namespace CarAppDomain.Rule.Repositories
{
    public class RuleMappingEntityRepository : BaseRepository<RuleMappingEntity>, IRuleMappingEntityRepository
    {
        private readonly ISqlSugarClient sqlSugarClient;
        public RuleMappingEntityRepository(ISqlSugarClient db):base(db)
        {
            sqlSugarClient = db;
        }

        public async Task<List<RuleMappingEntity>> GetRuleMappingByVINAsync(string ruleId, string VIN)
        {
            string sql = "";
            switch (((SqlSugar.SqlSugarScope)sqlSugarClient).CurrentConnectionConfig.DbType)
            {
                case DbType.MySql: sql = $"SELECT * FROM rule_mapping WHERE RuleId=@ruleId and  @vin REGEXP RegStr; "; break;
                case DbType.Oracle: sql = $" SELECT * FROM rule_mapping WHERE RuleId=@ruleId and REGEXP_LIKE(@vin, RegStr)"; break;
                case DbType.PostgreSQL: sql = $"SELECT * FROM rule_mapping WHERE RuleId=@ruleId and @vin ~ RegStr"; break;
                case DbType.Sqlite: sql = $"SELECT * FROM rule_mapping WHERE RuleId=@ruleId and @vin REGEXP  RegStr"; break;
                default: sql = $"SELECT * FROM rule_mapping WHERE RuleId=@ruleId and @vin LIKE  RegStr"; break;
            }
            return await sqlSugarClient.Ado.SqlQueryAsync<RuleMappingEntity>(sql, new { ruleId= ruleId,vin = VIN });
        }
    }
}
